package com.data.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.data.entity.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by rize on 2019/7/8.
 */
@Mapper
@Component
public interface AdminMapper extends BaseMapper<Admin> {

    //根据登录名查询
    @Select("SELECT a.id,a.username,a.avatar,a.password,a.realname,a.role_id roleId,a.gender,a.phone,a.status,a.createtime,a.updatetime, r.name as roleName FROM t_admin a " +
            "LEFT JOIN t_role r on r.id = a.role_id " +
            "where a.status=1 " +
            "and a.username=#{username} ")
    public Admin selectByUsername(String username);

    //根据id查询
    @Select("SELECT a.id,a.username,a.avatar,a.password,a.realname,a.role_id roleId,a.gender,a.phone,a.status,a.createtime,a.updatetime, r.name as roleName " +
            "FROM t_admin a " +
            "LEFT JOIN t_role r on r.id = a.role_id " +
            "where a.status=1 " +
            "and a.id=#{id} ")
    public Admin selectAdminById(Long id);

    //修改密码
    @Update("update t_admin set password=#{password} where id=#{id}")
    public int updatePwd(@Param("id") Long id, @Param("password") String password);


    @Select("SELECT a.id,a.username,a.avatar,a.password,a.realname,a.role_id,a.gender,a.phone,a.status,a.createtime,a.updatetime, r.name as roleName FROM t_admin a " +
            "LEFT JOIN t_role r on r.id = a.role_id " +
            "limit #{pageIndex},#{pageSize}")
    List<Admin> listPage(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize);
    //查询数量
    @Select("select count(1) from t_admin")
    int count();



    //获取登录名
    @Select("select * FROM t_admin where username=#{username}")
    Admin getByUsername(String username);



}
